﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Diagnostics;
using Newtonsoft.Json;
using Weibo4Sharp.Extensions;

namespace Weibo4Sharp.Model
{
    /// <summary>
    /// The weibo comment model
    /// </summary>
    [Serializable]
    [DataContract]
    [DebuggerDisplay("{Text}")]
    [JsonObject(MemberSerialization.OptIn)]
    public class WeiboComment : 
        IWeiboModel,
        IComparable<WeiboComment>,
        IEquatable<WeiboComment>
    {
        [DataMember]
        [JsonProperty("created_at")]
        public virtual DateTime CreatedDate { get; set; }

        [DataMember]
        public virtual long Id { get; set; }

        [DataMember]
        public virtual string Text { get; set; }

        private string _textAsHtml;

        [DataMember]
        public virtual string TextAsHtml
        {
            get
            {
                if (string.IsNullOrEmpty(Text))
                {
                    return Text;
                }
                
                return _textAsHtml ?? (_textAsHtml = Text.ParseWeiboageToHtml());
            }
            set
            {
                _textAsHtml = value;
            }
        }


        [DataMember]
        public virtual WeiboUser User { get; set; }

        [DataMember]
        public virtual WeiboStatus Status { get; set; }

        [DataMember]
        [JsonProperty("reply_comment")]
        public virtual WeiboComment ReplyComment { get; set; }

        public string RawSource { get; set; }   

        public int CompareTo(WeiboComment other)
        {
            return other.Id.CompareTo(this.Id);
        }

        public bool Equals(WeiboComment other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return other.Id == Id;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }

            if (ReferenceEquals(this, obj))
            {
                return true;
            }

            return obj.GetType() == typeof(WeiboComment) && Equals((WeiboComment)obj);
        }

        public override int GetHashCode()
        {            
            return Id.GetHashCode();
        }

        public static bool operator ==(WeiboComment left, WeiboComment right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(WeiboComment left, WeiboComment right)
        {
            return !(left == right);
        }

             
    }
}
